var useFiles = [];

useFiles.push('easyui');
useFiles.push('uikit');
useFiles.push('uikit.css');
useFiles.push('easyui.css');
useFiles.push('easyui-icon.css');
useFiles.push('page.css');
useFiles.push('easyui-locale');

seajs.use(useFiles, function() {
	$(initPage);
});

function initPage() {
	$('a[href="#ok"]').on('click', ok_click);
	
	$('a[href="#reset"]').on('click', reset_click);
	
	$('a[href="#back"]').on('click', back_click);
	
	$('a[href="#add"]').on('click', add_handler);
	
	$('a[href="#edit"]').on('click', edit_handler);
	
	$('a[href="#remove"]').on('click', remove_handler);
	
	$('a[href="#reloadTree"]').on('click', reloadTree_click);
}

function tree_onBeforeLoad(node, param) {
	if (param.id != undefined) {
		param["dictionaryTy.parentId"] = param.id;
	}
}

function tree_loadFilter(data, parent) {
	if (data.message != undefined) {
		$.messager.alert('提示消息', '树加载失败', 'info');
		
		return [ {
		    id : 'fand',
		    text : '根',
		    state : "open"
		} ];
	}
	
	if (parent) {
		if (data.dataList) {
			return data.dataList;
		}
		
		return data;
	}
	
	return [ {
	    id : 'fand',
	    text : '根',
	    state : "open",
	    children : data.dataList
	} ];
}

function tree_onSelect(node) {
	$('td[name^="dictionaryTy."]').each(function() {
		$this = $(this);
		
		var name = $this.attr('name');
		
		name = name.replace('dictionaryTy.', '');
		
		$this.html((node == undefined || node.attributes[name] == undefined) ? '' : node.attributes[name]);
	});
}

function tree_onDrop(target, source, point) {
	var targetId = $('.easyui-tree').tree('getNode', target).id;
	
	if (targetId == 'fand') {
		targetId = '';
	}
	
	var data = {
	    "dictionaryTy.parentId" : targetId,
	    "dictionaryTy.id" : source.id,
	    "dictionaryTy.pointCo" : point
	};
	
	var url = fand.webContextUrl + '/admin/configuration/dictionary/dictionaryTy/saveByMove.json';
	
	$.messager.progress();
	
	$.post(url, data, function(data) {
		$.messager.progress('close');
		
		if (data.message == undefined) {
			$.messager.alert('提示消息', '移动成功！', 'info', reloadTree_click);
			
			return;
		}
		
		$.messager.alert('提示消息', '删除失败:' + data.message, 'error');
	}, 'json');
	
	return false;
}

function reloadTree_click() {
	$('.easyui-tree').tree('reload');
	
	tree_onSelect();
}

function refurbishData() {
	reloadTree_click();
	
	$('.easyui-datagrid').datagrid('reload');
}

function ok_click() {
	queryParams['dictionaryTy.code'] = $('input[name="dictionaryTy.code"]').val();
	
	queryParams['dictionaryTy.value'] = $('input[name="dictionaryTy.value"]').val();
	
	queryParams['dictionaryTy.dictionaryCo'] = $('input[name="dictionaryTy.dictionaryCo"]').val();
	
	$('.easyui-datagrid').datagrid('reload');
	
	back_click();
}

function reset_click() {
	$('form').each(function() {
		this.reset();
	});
}

function back_click() {
	$windowForSearch = $('#windowForSearch');
	
	$windowForSearch.window('close');
}

function onDblClickRow(rowIndex, rowData) {
	var src = fand.webContextUrl + '/admin/configuration/dictionary/dictionaryTy/view.html?dictionaryTy.id='
	        + rowData.ID;
	
	$windowForIframe = $('#windowForIframe');
	
	$windowForIframe.find('iframe').prop('src', src);
	
	$windowForIframe.window({
	    title : '查看',
	    width : ($('html').width() * 0.95),
	    height : ($('html').height() * 0.95),
	    modal : true
	});
}

var queryParams = {};

function onBeforeLoad(param) {
	param['dictionaryTy.paginationNo'] = param.page;
	param['dictionaryTy.paginationSize'] = param.rows;
	
	$.extend(param, queryParams);
}

function add_handler() {
	var src = '';
	
	var href = $(this).attr('href');
	
	if ('#add' == href) {
		var node = $('.easyui-tree').tree('getSelected');
		
		if (node != null && node.id != 'fand') {
			src = fand.webContextUrl
			        + '/admin/configuration/dictionary/dictionaryTy/new.html?dictionaryTy.parentId=' + node.id;
		}
	} else {
		var row = $('.easyui-datagrid').datagrid('getSelected');
		
		if (row != null) {
			src = fand.webContextUrl
			        + '/admin/configuration/dictionary/dictionaryTy/new.html?dictionaryTy.parentId=' + row.ID;
		}
	}
	
	if (src == '') {
		src = fand.webContextUrl + '/admin/configuration/dictionary/dictionaryTy/new.html';
	}
	
	$windowForIframe = $('#windowForIframe');
	
	$windowForIframe.find('iframe').prop('src', src);
	
	$windowForIframe.window({
	    title : '新增',
	    width : ($('html').width() * 0.95),
	    height : ($('html').height() * 0.95),
	    modal : true
	});
	
	return false;
}

function edit_handler() {
	var id = '';
	
	var href = $(this).attr('href');
	
	if ('#edit' == href) {
		var node = $('.easyui-tree').tree('getSelected');
		
		if (node == null) {
			$.messager.alert('提示消息', '请选择需编辑的数据！', 'warning');
			
			return false;
		}
		
		if (node.id == 'fand') {
			$.messager.alert('提示消息', '根节点不允许编辑！', 'warning');
			
			return false;
		}
		
		id = node.id;
	} else {
		var row = $('.easyui-datagrid').datagrid('getSelected');
		
		if (row == null) {
			$.messager.alert('提示消息', '请选择需编辑的数据！', 'warning');
			
			return false;
		}
		
		id = row.ID;
	}
	
	var src = fand.webContextUrl + '/admin/configuration/dictionary/dictionaryTy/edit.html?dictionaryTy.id=' + id;
	
	$windowForIframe = $('#windowForIframe');
	
	$windowForIframe.find('iframe').prop('src', src);
	
	$windowForIframe.window({
	    title : '编辑',
	    width : ($('html').width() * 0.95),
	    height : ($('html').height() * 0.95),
	    modal : true
	});
	
	return false;
}

function remove_handler() {
	var ids = [];
	
	var href = $(this).attr('href');
	
	if ('#remove' == href) {
		var node = $('.easyui-tree').tree('getSelected');
		
		if (node == null) {
			$.messager.alert('提示消息', '请选择需删除的数据！', 'warning');
			
			return false;
		}
		
		if (node.id == 'fand') {
			$.messager.alert('提示消息', '根节点不允许删除！', 'warning');
			
			return false;
		}
		
		ids.push(node.id);
	} else {
		var rows = $('.easyui-datagrid').datagrid('getChecked');
		
		if (rows.length < 1) {
			$.messager.alert('提示消息', '请勾选需删除的数据！', 'warning');
			
			return false;
		}
		
		$(rows).each(function(i, row) {
			ids.push(row.ID);
		});
	}
	
	$.messager.confirm('确认', '将删除选择的数据?', function(r) {
		if (r) {
			var url = fand.webContextUrl + '/admin/configuration/dictionary/dictionaryTy/delete.json';
			
			$.messager.progress();
			
			$.post(url, {
				'dictionaryTy.ids' : ids.join(',')
			}, function(data) {
				$.messager.progress('close');
				
				if (data.message == undefined) {
					$.messager.alert('提示消息', '删除成功！', 'info');
					
					refurbishData();
					
					return;
				}
				
				$.messager.alert('提示消息', '删除失败:' + data.message, 'error');
			});
		}
	});
	
	return false;
}

function view_handler() {
	var row = $('.easyui-datagrid').datagrid('getSelected');
	
	if (row == null) {
		$.messager.alert('提示消息', '请选择需查看的数据！', 'warning');
		
		return false;
	}
	
	onDblClickRow((row.RN - 1), row);
}

function search_handler() {
	$windowForSearch = $('#windowForSearch');
	
	$windowForSearch.window({
	    width : ($('html').width() * 0.5),
	    height : ($('html').height() * 0.5),
	    modal : true
	});
	
	return false;
}

var toolbar = [ {
    text : '新增',
    iconCls : 'icon-add',
    handler : add_handler
}, {
    text : '编辑',
    iconCls : 'icon-edit',
    handler : edit_handler
}, '-', {
    text : '批量删除',
    iconCls : 'icon-remove',
    handler : remove_handler
}, '-', {
    text : '查看',
    iconCls : 'icon-tip',
    handler : view_handler
}, '-', {
    text : '查询',
    iconCls : 'icon-search',
    handler : search_handler
} ];